import { Body, Controller, HttpStatus, Param, ParseIntPipe, Post, Req, Res } from '@nestjs/common';
import { ChargingCardService } from './charging-card.service';
import { UtilsService } from '@app/utils';

@Controller('chargingCard')
export class ChargingCardController {
    constructor(
        private service:ChargingCardService,
        private utils:UtilsService
    ){}

    @Post('cards')
    async cards(@Body() query:Pagination, @Req() { userId }) {
        let rows = await this.service.cards(userId)
        return this.utils.successRes(rows)
    }

    @Post('bind')
    async bind(@Body('cardNo') cardNo:string, @Req() { userId }) {
        if(cardNo == undefined || cardNo == '') return
        const r = await this.service.bind(cardNo, userId)
        if(r == -1) return this.utils.failRes('卡已被使用')
        return r ? this.utils.successRes() : this.utils.failRes()
    }

    @Post('unbind')
    async add(@Body('id', ParseIntPipe) id:number, @Req() { userId }) {
        if(isNaN(id) || id < 1) return
        const r = await this.service.unbind(id, userId)
        return r ? this.utils.successRes() : this.utils.failRes()
    }

    @Post('recharge/record')
    async page(@Body() query:Pagination & {cardId:number}) {
        let rows = await this.service.rechargeRecord(query)
        return this.utils.successRes(rows)
    }


    @Post('recharge/pay')
    async rechargePay(@Body() dto:{cardId:number, optionId:number}, @Req() { user, userId, isWx, clientSource }) {
        if(dto.optionId < 1 || dto.cardId < 1) return
        const r = await this.service.rechargePay(dto.optionId, dto.cardId, userId, clientSource, isWx, user.openid)
        return r == null ? this.utils.failRes() : this.utils.successRes(r)
    }

    @Post('recharge/notify/:client/:id')
    async rechargeNotify(@Param('id', ParseIntPipe) id:number, @Param('client') client:string, @Body() data:any, @Res() res) {
        try {
            if(client == 'wx') {
                if(data.event_type == 'TRANSACTION.SUCCESS') {
                    const r = await this.service.rechargeNotify(id, data.resource, true)
                    if(r) {
                       res.status(HttpStatus.OK).send();
                    }else {
                        res.status(HttpStatus.NOT_IMPLEMENTED).send({code : 'FAIL', message : "失败"});
                    }
                }     
                
            }else { //支付宝
                
                if(data.trade_status == 'TRADE_SUCCESS') {
                    const r = await this.service.rechargeNotify(id, data, false)
                    if(r) {
                        res.status(HttpStatus.OK).send();
                     }else {
                         res.status(HttpStatus.NOT_IMPLEMENTED).send({code : 'FAIL', message : "失败"});
                     }
                }
                
            }

            return
        } catch (error) {
            
        }
        
        res.status(HttpStatus.NOT_IMPLEMENTED).send({code : 'FAIL', message : "失败"});
    }
}
